//! \file blob.hpp
//! \brief Purewell Library Blob Header
//! \author Yubin Lim (purewell@gmail.com)
//! \since 2009-11-26

#ifndef __PWHDR_BLOB_HPP__
#define __PWHDR_BLOB_HPP__

#include <string>

namespace purewell
{

//! \brief Blob
class PWAPI PWBlob
{
public:
	//! \brief Check ready to use.
	//! \return On success, return true.
	bool isReady(void) const;

	//! \brief Check ready to use buffer size.
	//! \param[in] blen Buffer size.
	//! \return On success, return true.
	bool isReady(size_t blen) const;

	//! \brief Swap blob.
	//! \param[in,out] Blob to swap.
	void swap(PWBlob& blob);

	//! \brief Get buffer.
	//! \return Buffer pointer.
	inline void* getData(void) { return m_buf; }

	//! \brief Get buffer.
	//! \return Buffer pointer.
	inline const char* getData(void) const { return m_buf; }

	//! \brief Get buffer size.
	//! \return Buffer size.
	inline size_t getSize(void) const { return m_size; }

	//! \brief Allocate buffer.
	//! \param[in] size Buffer size.
	//! \return On success, return true.
	bool allocate(size_t size);

	//! \brief Allocate buffer.
	//! \param[in] c Initilaizing character.
	//! \param[in] size Buffer size.
	//! \return On success, return true.
	bool allocate(char c, size_t size);

	//! \brief Allocate and copy buffer.
	//! \param[in] blob Copy buffer.
	//! \return On success, return true.
	bool allocate(const PWBlob& blob);

	//! \brief Allocate and copy buffer.
	//! \param[in] buf Source buffer.
	//! \param[in] blen Buffer length.
	//! \return On success, return true.
	bool allocate(const char* buf, size_t blen);

	//! \brief Allocate and copy buffer with null-end.
	//! \param[in] s Source buffer.
	//! \return On success, return true.
	bool allocate(const std::string& s);

	//! \brief Reallocate buffer.
	//! \param[in] size Buffer size.
	//! \return On success, return true.
	bool reallocate(size_t size);

	//! \brief Free buffer.
	void free(void);

	//! \brief Assign buffer. No allocation operation.
	//! \param[in] blob Source buffer.
	//! \return On success, return true.
	bool assign(const PWBlob& blob);

	//! \brief Assign buffer. No allocation operation.
	//! \param[in] spos Start position.
	//! \param[in] blob Source buffer.
	//! \param[in] sspos Source buffer start position.
	//! \param[in] size Size to assign.
	//! \return On success, return true.
	bool assign(size_t spos, const PWBlob& blob,
		size_t sspos, size_t size=(size_t)-1);

	//! \brief Assign buffer. No allocation operation.
	//! \param[in] buf Source buffer.
	//! \param[in] blen Source buffer length.
	//! \return On success, return true.
	bool assign(const char* buf, size_t blen);

	//! \brief Assign buffer. No allocation operation.
	//! \param[in] spos Start position of destination buffer.
	//! \param[in] buf Source buffer.
	//! \param[in] blen Source buffer length.
	//! \return On success, return true.
	bool assign(size_t spos, const char* buf, size_t blen);

	//! \brief Assign buffer. No allocation operation.
	//! \param[in] c Initializing character.
	//! \param[in] spos Start position.
	//! \param[in] size Size to assign.
	//! \return On success, return true.
	bool assign(char c, size_t spos=0, size_t size=(size_t)-1);

	//! \brief Assign buffer. No allocation operation.
	//! \param[in] s Source buffer. (with null-end)
	//! \return On success, return true.
	bool assign(const std::string& s);

	//! \brief Assign buffer. No allocation operation.
	//! \param[in] spos Start position.
	//! \param[in] s Source buffer.
	//! \param[in] sspos Source buffer start position.
	//! \param[in] size Size to assign.
	//! \return On success, return true.
	bool assign(size_t spos, const std::string& s, size_t sspos,
		size_t size=(size_t)-1);

public:
	//! \brief Assign operation. (Allocate and assign)
	PWBlob& operator = (const PWBlob& blob);

	//! \brief Assign operation. (Allocate and assign)
	PWBlob& operator = (const std::string& s);

public:
	explicit PWBlob();
	explicit PWBlob(size_t blen);
	explicit PWBlob(char c, size_t blen);
	explicit PWBlob(const char* buf, size_t blen);

	//! \brief Constructor. Copy from STL string with null-end.
	explicit PWBlob(const std::string& s);
	explicit PWBlob(const PWBlob& blob);

	virtual ~PWBlob();

protected:
	char*		m_buf;		//!< Buffer.
	size_t		m_size;		//!< Buffer size.
};

};//namespace purewell

#endif//!__PWHDR_BLOB_HPP__

